package cn.lyq.card.ui.rank

import android.animation.ValueAnimator
import android.view.View
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import cn.lyq.card.R
import cn.lyq.card.base.BaseFg
import cn.lyq.card.data.src.Status
import cn.lyq.card.utils.bindViewPager
import cn.lyq.card.utils.setClick
import cn.lyq.card.utils.setSimpleData
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.fg_rank.*
import kotlinx.android.synthetic.main.layout_tab_normal.*
import kotlinx.android.synthetic.main.layout_title.*


class RankFg : BaseFg() {

    private val mModel by lazy {
        getVMActivity(RankVM::class.java)
    }
    private val mPageAdapter by lazy {
        PagerAdapter(requireActivity())
    }
    private val mTypeAdapter by lazy {
        RankTypeAdapter()
    }
    private val mTypeHorAdapter by lazy {
        RankTypeHorAdapter()
    }
    private var anim: ValueAnimator? = null

    override fun getLayoutId(): Int {
        return R.layout.fg_rank
    }

    override fun initView(view: View) {
        super.initView(view)
        tvTitle.setText(R.string.str_rank_list)
        imgBack.visibility = View.GONE
        tvTitleSplit.visibility = View.GONE

        viewPager.adapter = mPageAdapter
        tabLayout.setSimpleData(arrayOf("日榜", "月榜", "总榜"))
        tabLayout.bindViewPager(viewPager)

        rvType.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
        rvType.adapter = mTypeAdapter

        rvExpendType.layoutManager = GridLayoutManager(requireContext(), 3)
        rvExpendType.adapter = mTypeHorAdapter

        mTypeAdapter.setOnItemClickListener { _, _, position ->
            if (mTypeAdapter.update(position)) {
                mModel.mTypeInfo.postValue(mTypeAdapter.data[position])
            }
        }
        mTypeHorAdapter.setOnItemClickListener { _, _, position ->
            if (mTypeAdapter.update(position)) {
                mModel.mTypeInfo.postValue(mTypeAdapter.data[position])
                rvType.scrollToPosition(position)
            }
            clType.visibility = View.GONE
            startAnim(false)
        }
    }

    private fun startAnim(isExpand: Boolean) {
        anim?.cancel()
        val start = imgLower.rotation
        val offset = (if (isExpand) 270 else 90) - start
        anim = ValueAnimator.ofFloat(1f).apply {
            addUpdateListener {
                val current = it.animatedValue as Float
                imgLower.rotation = start + offset * current
            }
            duration = 500
        }
        anim?.start()
    }

    override fun initListener(view: View) {
        super.initListener(view)
        clType.setClick {
            clType.visibility = View.GONE
            startAnim(false)
        }
        tvLower.setClick {
            if (clType.visibility == View.GONE) {
                val location = mutableListOf(0, 0).toIntArray()
                tvSplitTab.getLocationOnScreen(location)
                clType.setPadding(0, location[1], 0, 0)
                clType.visibility = View.VISIBLE
                startAnim(true)
            }
        }
        tvTypeBg.setClick {
            clType.visibility = View.GONE
            startAnim(false)
        }
    }

    override fun initObserver() {
        super.initObserver()
        mModel.getRankType().observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> {
                    val list = it.requireData()
                    mTypeAdapter.setList(list)
                    mTypeHorAdapter.setList(list)
                    if (list.size > 0) {
                        mModel.mTypeInfo.postValue(list[0])
                    }
                    dismissLoading()
                }
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
            }
        })
    }

    private class PagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

        override fun getItemCount(): Int = 3

        override fun createFragment(position: Int): RankSubFg {
            return RankSubFg.newInstance(position)
        }

    }
}